Ext.define("Triends.Model.Userinfo",{
	extend: "Ext.data.Model",
	config: {
		fields: [
			"ID", "Name", "Email", "Age", "Gender", "Introduction", "Cacao", "UserLat", "UserLng", "UserTheme", "Total", "Wifi"
		]
	}
});

var UserStore = Ext.create("Ext.data.Store",{
	model: "Triends.Model.Userinfo",
	id: 'UserStore',
	proxy: {
		type: "jsonp",
		callbackKey: 'callback',
		url: "http://itscreater.cafe24.com/Triends/Userinfo.jsp"
	}
});

Ext.define('Triends.view.Userinfo', {
 	extend: 'Ext.Panel',
 	requires: ['Triends.view.Login'],
 	id: 'userinfo',
 	xtype: 'userinfo',
	config: {
		style: 'position:relative; background:white',
 		layout: 'vbox',
 		fullscreen: true,
 		scrollable: true,
 		items: [
 			{
 				xtype: 'titlebar',
 				docked: 'top',
 				ui: 'light',
 				items: [{
 					xtype: 'button',
 					align: 'right',
 					height: 35,
	 				iconCls: 'settings',
	 				iconMask: true,
	 				handler: function() {
	 					var cacao = Ext.getStore('UserStore').getAt(0).data.Cacao;
	 					var introduction = Ext.getStore('UserStore').getAt(0).data.Introduction;
	 					var usercacao = Ext.getCmp('usercacao');
	 					var userintroduction = Ext.getCmp('userintroduction');
	 					usercacao.setValue(cacao);
	 					
	 					if (introduction != null){
		 					if (introduction.match("<br>") != null || introduction.match("&nbsp;") != null){
								introduction = introduction.replace(/&nbsp;/gi, " "); /*/gi를 써서 전체 문자열을 바꿔준다 */
								introduction = introduction.replace(/<br>/gi, "\n");
							}
	 					}
	 					userintroduction.setValue(introduction);
	 					Triends.app.getHistory().add(Ext.create('Ext.app.Action', {
        					url: 'mainpage/account/infomodify'
    					}));
	 				}
 				},
 				{
 					
						xtype: 'button',
						align: 'right',
						height: 35,
						text: 'Logout',
						handler: function(){
							function msgFunction(buttonIndex){
								if (buttonIndex == 1) {
//			   						var userid = Ext.getStore('UserStore').getAt(0).data.ID;
									FB.logout();  
									window.GCM.unregister(GCM_Success, GCM_Fail);
									Ext.data.JsonP.request({
										url: 'http://itscreater.cafe24.com/Triends/gcmdelete.jsp',
										params: {regid: deviceID},
										callbackKey: 'callback'
									});
									Ext.getStore('UserStore').removeAll();
									Triends.app.getHistory().add(Ext.create('Ext.app.Action', {
		            					url: 'login'
			        				}));	
		   						} else {
		   							Ext.emptyFn();
		   						}
							}
							navigator.notification.confirm(
								'로그아웃 하시겠습니까?',
								msgFunction,
								'알림',
								'OK, Cancel'
							)
						}
 				}]
 			},
 			{
	 			xtype: 'panel',
	 			layout: 'hbox',
		 		height: 200,
	 			style: {'padding':'15px', 'border-bottom':'1px #CBCAC7 solid'},
	 			items: [
 				{
	 				xtype: 'panel',
	 				flex: 1,
	 				items:[{
	 					xtype: 'panel',
	 					style: 'left:50%;margin-left:-70px;padding-top:15px;',
						id: 'userimg',
						width: 130,
						height: 130
 					}]
	 			},
	 			{
	 				xtype: 'panel',
	 				flex: 1.5,
	 				layout: 'vbox',
	 				items:[{
	 					xtype: 'dataview',
	 					store: UserStore,
	 					margin: 10,
	 					scrollable: false,
	 					loadingText: false,
	 					itemTpl: "<div align='left'><b><font size='4'>{Name}</font></b></div>"
	 				},
	 				{
	 					xtype: 'panel',
	 					id: 'userlocationpanel',
	 					style: 'margin-left:10px;margin-top:-10px'
	 				},
	 				{
	 					xtype: 'spacer',
	 					height: 28
	 				},
					{
						xtype: 'panel',
						width: '100%',
						style: 'margin-bottom:5px',
						layout: 'hbox',
						items: [
		 					{
		 						xtype: 'image',
		 						style: 'margin-left:18px',
		 						src: 'resource/images/cacao.png',
		 						width: 24,
		 						height: 24
		 					},
		 					{
			 					xtype: 'dataview',
			 					store: UserStore,
			 					style: 'margin-left: 10px',
			 					scrollable: false,
			 					loadingText: false,
			 					itemTpl: "<font size='3'>{Cacao}</font>"
		 					}
						]
	 				},
	 				{
	 					xtype: 'panel',
	 					style: 'padding-right:10px',
	 					items: [{
	 						xtype: 'button',
	 						width: '100%',
	 						style: 'border: 2px outset;margin-left:10px',
			 				id: 'msgboxbtn',
			 				ui: 'small',
			 				text: 'Message Box',
			    		   	iconCls: 'mail',
			    	 	   	iconMask: true,
			   			   	handler: function() {
			   			   		Ext.Viewport.setMasked({
    								xtype: 'loadmask',
    								indicator: true
    							});
			   			   		var userid = Ext.getStore('UserStore').getAt(0).data.ID;
	    						var msgStore = Ext.getStore('msgstore');
	            				msgStore.getProxy().setExtraParams({id: userid});
	            				msgStore.load({
	            					callback: function() {
										Triends.app.getHistory().add(Ext.create('Ext.app.Action', {
	            							url: 'mainpage/account/mymsg'
		        						}));	
		        						Ext.Viewport.setMasked(false);
	            					}
	            				});
	            			}	
	 					}]
	 				}
 				]
 			}]
 		},
 		{
 			xtype: 'panel',
 			layout: 'vbox',
 			height: 100,
 			style: 'padding:15px;border-bottom:1px #CBCAC7 solid',
 			items: [{
				xtype: 'panel',
				html: "<p style='margin-left:5px'><font size='4'><b>Theme</b></font></p>"
			},
 			{
 				xtype: 'panel',
 				width: '100%',
 				items: [{
 					xtype: 'image',
		 			id: 'userthemepanel',
		 			centered: true,
 					width: 48,
 					height: 48
 				}]
 			}]
 		},	
 		{
 			xtype: 'panel',
 			layout: 'vbox',
 			style: {'padding':'15px', 'border-bottom':'1px #CBCAC7 solid'},
 			minHeight: 100,
 			items: [
 				{
					xtype: 'panel',
					style: 'margin-bottom:10px',
					html: "<p style='margin-left:5px'><font size='4'><b>Introduction</b></font></p>"
				},
				{
					xtype: 'dataview',
					scrollable: false,
					store: UserStore,
					loadingText: false,
					itemTpl:"<font size='3'>{Introduction}</font>"
				}
 			]
 		},
		{
				xtype: 'usertinglist'
		}]
//		{
//			
//	 			xtype: 'dataview',
//	 			minHeight: 200,
//	 			scrollable: false,
//				store:  UserStore,
//				itemTpl: 'Age: {Age} <br>' +
//						 'Gender: {Gender} <br>' +		
//						 'Nationality: {Nationality} <br>' +
//						 'Work: {Work} <br>' +
//						 'Education: {Education} <br>' +
//						 'Love: {Love}',
//				items: [{
	 					
//	 				}	
//				}]	
//			},
		
	}
});

